package com.jinly.ryder.account.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.account.service.DingtalkService;
import com.jinly.ryder.account.service.UserAuthService;
import com.jinly.ryder.account.service.UserInfoService;
import com.jinly.ryder.account.service.model.UserAuth;
import com.jinly.ryder.account.service.model.UserInfo;
import com.jinly.ryder.account.web.mapping.DingTalkDeptVOMapping;
import com.jinly.ryder.account.web.mapping.UserAuthVOMapping;
import com.jinly.ryder.account.web.mapping.UserInfoVOMapping;
import com.jinly.ryder.account.web.vo.DingTalkDeptVO;
import com.jinly.ryder.account.web.vo.UserAuthVO;
import com.jinly.ryder.account.web.vo.UserInfoVO;
import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("userInfo")
@RequiredArgsConstructor
public class UserInfoController {

    private final UserInfoService userInfoService;
    private final UserAuthService userAuthService;
    private final DingtalkService dingtalkService;
    private final UserInfoVOMapping userInfoVOMapping;
    private final UserAuthVOMapping userAuthVOMapping;
    private final DingTalkDeptVOMapping dingTalkDeptVOMapping;

    @GetMapping("page")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public Page<UserInfoVO> page(Long current, Long size, UserInfoVO userInfoVO) {
        return userInfoVOMapping.toVOPage(userInfoService.queryUserPage(new Page(current, size), userInfoVOMapping.toModel(userInfoVO)));
    }

    @GetMapping("{userId}")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public UserInfoVO get(@PathVariable Long userId) {
        return userInfoVOMapping.toVo(userInfoService.getUserInfo(userId));
    }

    @PostMapping
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public void addUser(@RequestBody UserInfoVO userInfoVO) {
        UserInfo userInfo = userInfoVOMapping.toModel(userInfoVO);
        userInfoService.addUserInfo(userInfo);
    }

    @GetMapping("userAuth/list")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public List<UserAuthVO> userAuthList(Long userId) {
        List<UserAuth> list = userAuthService.queryUserAuthListByUserId(userId, null);
        return userAuthVOMapping.toVoList(list);
    }

    @DeleteMapping("userAuth")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public void deleteUserAuth(Long authId) {
        userAuthService.deleteUserAuth(authId);
    }

    @PostMapping("userAuth")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public void addUserAuth(@RequestBody UserAuthVO userAuthVO) {
        userAuthService.addUserAuth(userAuthVO.getUserId(), userAuthVO.getAuthType(), userAuthVO.getAuthRole(), userAuthVO.getAuthData());
    }

    @GetMapping("dingTalk/deptList")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public List<DingTalkDeptVO> dingTalkDeptList() {
        return dingTalkDeptVOMapping.toVoList(dingtalkService.getDeptList());
    }

    @GetMapping("dingTalk/deptUserList")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public List<UserInfoVO> dingTalkDeptUserList(Long deptId) {
        List<UserInfoVO> list = userInfoVOMapping.dingtalkToVoList(dingtalkService.getUserListByDeptId(deptId));
        list = list.stream().filter(u -> !ObjectUtils.isEmpty(u.getEmail())).collect(Collectors.toList());
        list.forEach(u -> u.setUsername(u.getEmail().split("@")[0]));
        List<UserInfo> userInfoList = userInfoService.batchQueryUsersByUserName(list.stream().map(UserInfoVO::getUsername).collect(Collectors.toSet()));
        list.forEach(u -> userInfoList.stream().filter(info -> info.getUsername().equals(u.getUsername())).findFirst().ifPresent(info -> u.setId(info.getId())));
        return list;
    }

    @GetMapping("dingTalk/userDetail")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "user")
    public UserInfoVO dingTalkDeptUserList(String userId) {
        return userInfoVOMapping.dingtalkToVo(dingtalkService.getUserInfo(userId));
    }
}
